<?php  if ( ! defined('ROOT_PATH')) exit('非法访问！！！');
/********************************************************
 Author: BurtonQ
Version: 1
 Effect: 
   Date: 
  Notes: 
********************************************************/
class get_byline_list
{
	private $aConfig = array();
	private $aCommConfiger = array();
	private $gaTools = array();
	private $aaGatherHandleError = array();
	private $aParameter = array();
	function __construct()
	{
		$this->aCommConfiger = &get_config('configer');
		$this->aParameter = get_parameter();
		global $gaTools;
		$this->gaTools = $gaTools;
		$this->set_config();
		//出错处理需要的参数
		$this->aaGatherHandleError['error_template']['con'] = $this->aConfig['error_template']['con'];
		$this->aaGatherHandleError['error_template']['search_replace'] = $this->aConfig['error_template']['search_replace'];
		$this->aaGatherHandleError['log_email'] = $this->gaTools['log_email'];
		$this->aaGatherHandleError['logs_path'] = LOGS_PATH;
	}
	
	function execute()
	{
		die('hi~');
		$aIndex = array($this->aCommConfiger['url_which'],$this->aCommConfiger['argv_which']);
		$asHave = final_parameter($aIndex,$this->aParameter,$this->aConfig['have']);
		$sHtmlCharset = isset($this->aConfig['html_charset']) ? $this->aConfig['html_charset'] : DEFAULT_CHARSET;

		$asCollectionResult = array();
		//开始循环抓每个
		if (!empty($asHave)) {
			foreach ($asHave as $k=>$v) {
				if ($sHtmlCharset) {
					$asTmp = $this->filter_page($v,$sHtmlCharset);
					if (!empty($asTmp)) {
						$asCollectionResult[$v['city_flag']] = $asTmp;
					}
				}
			}
			if ($asCollectionResult) {
				require_once('bus_company_db.php');
				$oTheDB = new bus_company_db();
				$status = $oTheDB->insert_needdb($asCollectionResult);
				if ($status) {
					echo 'bus_company_agent 采集成功';
				}
			}
		}
	}
	
	private function filter_page($aHave,$sHtmlCharset)
	{
		$asCollectionResult = null;
		$sDateFormat = date('Y-m-d');
		$sName = $aHave['name'];
		$sUrl = $aHave['url'];
		$sHtml = meclient($sUrl);
		if (empty($sHtml)) {
			#记录发送邮件并记录日志
			$sCfile = __FILE__;
			$sCline = __LINE__;
			$sLogUrl = $sUrl;
			$sLogName = $sName;
			gather_handle_error($sLogName,$sLogUrl,$sCfile,$sCline,$this->aaGatherHandleError);
			return;
		}
		$domHtml = str_get_html($sHtml);
		if (empty($domHtml)) {
			#记录发送邮件并记录日志
			$sCfile = __FILE__;
			$sCline = __LINE__;
			$sLogUrl = $sUrl;
			$sLogName = $sName;
			gather_handle_error($sLogName,$sLogUrl,$sCfile,$sCline,$this->aaGatherHandleError);
			return;
		}
		$domTmp = $domHtml->find('.left',0);
		if (empty($domTmp)) {
			#记录发送邮件并记录日志
			$sCfile = __FILE__;
			$sCline = __LINE__;
			$sLogUrl = $sUrl;
			$sLogName = $sName;
			gather_handle_error($sLogName,$sLogUrl,$sCfile,$sCline,$this->aaGatherHandleError);
			return;
		}
		$domTmp = $domTmp->find('.ka',0);
		if (empty($domTmp)) {
			#记录发送邮件并记录日志
			$sCfile = __FILE__;
			$sCline = __LINE__;
			$sLogUrl = $sUrl;
			$sLogName = $sName;
			gather_handle_error($sLogName,$sLogUrl,$sCfile,$sCline,$this->aaGatherHandleError);
			return;
		}
		$domTmp = $domTmp->find('li');
		if (empty($domTmp)) {
			#记录发送邮件并记录日志
			$sCfile = __FILE__;
			$sCline = __LINE__;
			$sLogUrl = $sUrl;
			$sLogName = $sName;
			gather_handle_error($sLogName,$sLogUrl,$sCfile,$sCline,$this->aaGatherHandleError);
			return;
		}
		$i = 0;
		foreach ($domTmp as $v) {
			$sBusCompany = remove_invalid(iconv($sHtmlCharset,DEFAULT_CHARSET_ICONV,$v->plaintext));
			$aTmp = explode("(",$sBusCompany);
			$sBusCompany = $aTmp[0];
			
			$domTmp2 = $v->find('a',0);
			if (empty($domTmp2)) {
				$domTmp2 = $v->find('span',0);
				if (empty($domTmp2)) {
					continue;
				}
				$sBusCompanyUrl = $aHave['url'];
			} else {
				$sBusCompanyUrl = $aHave['for_rooturl'] . $domTmp2->href;
			}
			
			$asCollectionResult[$i]['company'] = $sBusCompany;
			$asCollectionResult[$i]['url'] = $sBusCompanyUrl;
			$i++;
		}
		$domHtml->__destruct();
		return $asCollectionResult;
	}
	
	function get_config()
	{
		return $this->aConfig;
	}
	private function set_config()
	{
		#采集出错信息模板
		$this->aConfig['error_template']['con'] = '采集“@sName”“@sGoUrl”出错，在脚本：“@sCfile”，的第“@sCline”行附近';
		$this->aConfig['error_template']['search_replace'] = array('@sName','@sGoUrl','@sCfile','@sCline');
		
		$this->aConfig['html_charset'] = 'gbk';
		
		$this->aConfig['have'] = &get_config('citys_info');
	}
	
	function __destruct()
	{
		
	}
}
